//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Text;
using System.IO;
using System.Drawing;

namespace DNAide.Core
{

	/// <summary>
	/// Methods to return embedded resource values
	/// </summary>
	public static partial class ResourcesAide
	{

		/// <summary>
		/// Returns DNAide text file embedded resource
		/// </summary>
		/// <param name="resourcePath">Resource path</param>
		/// <returns>String</returns>
		public static String GetEmbeddedDNAideTextFileResource(string resourcePath)
		{
			string retval = "";
			using (StreamReader reader = new StreamReader(GetEmbeddedDNAideResourceAsStream(resourcePath)))
			{
				retval = reader.ReadToEnd();
			}
			return retval;
		}


		/// <summary>
		/// Returns DNAide image embedded resource
		/// </summary>
		/// <param name="resourcePath">Resource path</param>
		/// <returns>Bitmap</returns>
		public static Bitmap GetEmbeddedDNAideImageResource(string resourcePath)
		{
			Stream sr = GetEmbeddedDNAideResourceAsStream(resourcePath);
			return new Bitmap(sr);
		}


		/// <summary>
		/// Retrns DNAide embedded resource
		/// </summary>
		/// <param name="resourcePath">Resource path</param>
		/// <returns>Stream</returns>
		public static Stream GetEmbeddedDNAideResourceAsStream(string resourcePath)
		{
			string assemblyName = typeof(ResourcesAide).Assembly.FullName.Substring(0, typeof(ResourcesAide).Assembly.FullName.IndexOf(","));
			try
			{
				System.Reflection.Assembly a = System.Reflection.Assembly.Load(assemblyName);
				Stream retval = a.GetManifestResourceStream(resourcePath);
				if (retval == null)
				{
					throw new Exception(string.Format(Resources.ResourceAide.UnableToLocateTextFileResource, resourcePath));
				}
				return retval;
			}
			catch (Exception e)
			{
				throw new Exception(assemblyName + ": " + e.Message);
			}
		}

	}
}
